<?php

class Media_ajax extends Admin_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->sSkinFile = 'ajax';
	}
	
	

	
	public function index()
	{
		return null;
	}
	
	
	public function get_folder_media()
	{
		
		$iFolderId = $this->input->post('iFolderId', true, 0);
		
		/*******************************
		 * Access rights check - start *
		 *******************************/	
		$this->access_rights->check_media_folder_access($this->user->userId, $this->oSecGroup->group_id, $iFolderId, 'read');
		/*****************************
		 * Access rights check - end *
		 *****************************/	

		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		$this->vw_media_by_folders->folder_id = $iFolderId;	
		
		
		$aOptions = array();
		$aOptions[] = array('where' => array('dimension_type', 'W', true, 1, 0));
		$aOptions[] = array('or_where' => array('dimension_type IS NULL', null, true, 0, 1));
		
		$aOptions[] = array('order_by' => array('media_id', 'desc'));
		
		$qMedia = $this->vw_media_by_folders->get($aOptions);
		$iMedia = $qMedia->num_rows();
		
		
		$this->aData['qMedia'] = $qMedia;
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/media/ajax/folder_media', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent);
		
		$this->aData['json_encode'] = array('sContent' => $sContent, 'iMedia' => $iMedia);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	


	public function set_uploaded_media_folder_id() 
	{
		$iFolderId = $this->input->post('iFolderId', true, 0);
		
		
		log_message('debug', 'setting folder id for null folders');
		$this->load->model('db/media', 'media');
		
		$this->media->folder_id = null;
		$this->media->user_id = $this->user->userId;
		
		$this->media->update_null_folders($iFolderId);
		
		$this->aData['json_encode'] = array('sComplete' => 'Y');
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function get_media_props()
	{
		$iMediaId = $this->input->post('iMediaId', true, 0);
		$sDimensionType = $this->input->post('sDimensionType', true, 'W');
		
		$this->load->model('db/views/media_by_folders', 'media_by_folders');
		
		$this->media_by_folders->media_id = $iMediaId;
		
		$aOptions = array();
		$aOptions[] = array('where' => array('dimension_type', $sDimensionType, true, 1, 0));
		$aOptions[] = array('or_where' => array('dimension_type IS NULL', null, true, 0, 1));
		
		
		$qMedia = $this->media_by_folders->get($aOptions, 1);
		

		if ($qMedia->num_rows() === 0)
		{
			//TODO: throw an error	
		}
		else 
		{
			$this->load->view('platform/'.$this->sPlatform.'/admin/media/ajax/media_props', array('oMedia' => $qMedia->row(0)));
		}
		
			
	}
	
	
	
	public function get_folder_form() 
	{
		$iParentFolderId = $this->input->post('iParentFolderId', true, '');
		$iFolderId = $this->input->post('iFolderId', true, '');
		$sAction = $this->input->post('sAction', true, 'add');
		$sOverlayId = $this->input->post('sOverlayId', true, '');
		
		$this->aData['iParentFolderId'] = $iParentFolderId;
		$this->aData['iFolderId'] = $iFolderId;
		$this->aData['sAction'] = $sAction;
		$this->aData['sOverlayId'] = $sOverlayId;
		
		
		$this->load->model('db/media_folders', 'media_folders');
		
		$this->media_folders->folder_id = $iFolderId;

		$qMediaFolder = $this->media_folders->get(array(), 1);
		
		if ($qMediaFolder->num_rows() === 0) 
		{
			$oMediaFolder = $this->media_folders->get_object();	
		}
		else 
		{
			$oMediaFolder = $qMediaFolder->row(0);	
		}
		
		$this->aData['oMediaFolder'] = $oMediaFolder;
		
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/media/folder_form', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
			
	}
	


	public function add_folder()
	{
		$iParentFolderId = (int) $this->input->post('iParentFolderId', true, 0);
		$sFolderTitle = $this->input->post('sFolderTitle', true, '');
		
		if ($iParentFolderId === 0)
		{
			$iParentFolderId = null;	
		}
		
		$this->load->model('db/media_folders', 'media_folders');
		
		$this->media_folders->folder_parent_id = $iParentFolderId;
		$this->media_folders->folder_title = $sFolderTitle;
		$this->media_folders->user_id = $this->user->userId;
		$this->media_folders->folder_epoch = time();
		
		$iFolderId = $this->media_folders->save();
		
		$this->load->library('media_lib');
		$this->load->model('db/media_folders', 'media_folders');
		
		$qFolders = $this->media_lib->get_folder_hierarchy(array('folder_parent_id' => NULL), array());
		
		$sMediaFolderTree = $this->media_lib->build_folder_hierarchy($qFolders, 'ul', 'folder_container', 'li', 'folder', true, '#');
		
		
		$sContent = $this->jsonify_string($sMediaFolderTree);
		
		$aJson = array('sContent' => $sContent);
		
		if ($iParentFolderId !== null)
		{
			$aJson['iParentFolderId'] = $iParentFolderId;	
		}
		
		$this->aData['json_encode'] = $aJson;
		
		$this->load->view('php_json_encode', $this->aData);
				
	}
	


	public function rename_folder()
	{
		$iFolderId = (int) $this->input->post('iFolderId', true, 0);
		$sFolderTitle = $this->input->post('sFolderTitle', true, '');
		
		$this->load->model('db/media_folders', 'media_folders');
		
		$this->media_folders->folder_id = $iFolderId;
		$this->media_folders->folder_title = $sFolderTitle;
		
		$this->media_folders->save();
		

		$aJson = array('iFolderId' => $iFolderId, 'sFolderTitle' => $sFolderTitle);
		
		$this->aData['json_encode'] = $aJson;
		
		$this->load->view('php_json_encode', $this->aData);
				
	}
	
	
	public function check_delete_folder()
	{
		$iFolderId = (int) $this->input->post('iFolderId', true, 0);
		
		$this->load->model('db/media', 'media');
		$this->media->folder_id = $iFolderId;
		
		$qMedia = $this->media->get(1);
		
		$iMedia = $qMedia->num_rows();
		
		$this->load->model('db/media_folders', 'media_folders');
		$this->media_folders->folder_parent_id = $iFolderId;
		
		$qChildFolders = $this->media_folders->get(1);
		
		$iChildFolders = $qChildFolders->num_rows();
		
		$aJson = array('iFolderId' => $iFolderId, 'iMedia' => $iMedia, 'iChildFolders' => $iChildFolders);
		
		$this->aData['json_encode'] = $aJson;
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	public function confirm_delete_folder()
	{
		$iFolderId = (int) $this->input->post('iFolderId', true, 0);
		
		$sDone = 'N';
		
		$this->load->model('db/media', 'media');
		$this->media->folder_id = $iFolderId;
		
		$qMedia = $this->media->get(1);
		
		if($qMedia->num_rows() === 0)
		{
			$this->load->model('db/media_folders', 'media_folders');
			$this->media_folders->folder_id = $iFolderId;
			
			$this->media_folders->delete();
			
			$sDone = 'Y';
		}
		
		$aJson = array('sDone' => $sDone);
		
		$this->aData['json_encode'] = $aJson;
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function rename_media()
	{
		$iMediaId = (int) $this->input->post('iMediaId', true, 0);
		$sMediaTitle = $this->input->post('value', true, '');
		
		if ($sMediaTitle !== '')
		{
			$this->load->model('db/media', 'media');
			
			$this->media->media_id = $iMediaId;
			$this->media->media_title = $sMediaTitle;
			
			$this->media->save();
		}	
		
		
		
		$aJson = array('sMediaTitle' => $sMediaTitle);
		
		$this->aData['json_encode'] = $aJson;
		
		$this->load->view('php_json_encode', $this->aData);
	}



	public function delete_media()
	{
		$iMediaId = (int) $this->input->post('iMediaId', true, 0);
		
		
		$aSearch = $this->search_content_for_media($iMediaId);	
		$iMatches = 0;
		
		if ($aSearch !== false)
		{
			$iMatches = $aSearch['qMatches']->num_rows();
		}

		$aJson = array('iMatches' => $iMatches);
		
		$this->aData['json_encode'] = $aJson;
		
		$this->load->view('php_json_encode', $this->aData);
	}



	public function confirm_delete_media()
	{
		$iMediaId = (int) $this->input->post('iMediaId', true, 0);
		
		
		
		$aSearch = $this->search_content_for_media($iMediaId);	
		
		if ($aSearch !== false)
		{
		
			$oMedia = $aSearch['oMedia'];
			
			
			$this->load->model('db/media_dimensions', 'media_dimensions');
			
			$this->media_dimensions->delete_by_media_id($oMedia->media_id);
		
		
			foreach ($this->config->item('admin_media_file_dims') as $sType => $aParams) 
			{
				$sFile = $this->config->item('admin_media_dir_clean').$aParams[0].'/'.$oMedia->media_subfolder.'/'.$oMedia->media_id.'.'.$oMedia->extension_ext;	
				
				
				@unlink($sFile);
			}
			
			
			$this->load->model('db/media', 'media');
		
			$this->media->media_id = $oMedia->media_id;
			$this->media->delete();
		}
		
		
		$aJson = array('sDone' => 'Y');
		
		$this->aData['json_encode'] = $aJson;
		
		$this->load->view('php_json_encode', $this->aData);
	}


	protected function search_content_for_media($iMediaId)
	{
		
		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 1) 
		{
			$this->load->model('db/views/pages_extended_content', 'vw_pages_extended_content');
				
			$sSearch = $this->config->item('admin_media_dir_url').'web/'.$qMedia->row(0)->media_subfolder.'/'.$qMedia->row(0)->media_id.'.'.$qMedia->row(0)->extension_ext;
			
			$aOptions = array();
			$aOptions[] = array('like' => array('page_content', $sSearch, 'BOTH'));
			
			$qMatches = $this->vw_pages_extended_content->get($aOptions);
			
			return array('qMatches' => $qMatches, 'oMedia' => $qMedia->row(0));	
			
		}
		
		return false;	
	}


	public function return_image_lib_errors()
	{
		$sError = $this->image_lib->display_errors();	
			
		$sError = $this->jsonify_string($sError);
		
		$this->aData['json_encode'] = array('sError' => $sError);
		
		$this->load->view('php_json_encode', $this->aData);
	}


	public function get_image_crop()
	{
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/media/edit_tools/image_crop', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	public function apply_crop()
	{
		$sSrcImage = $this->input->post('sSrcImage', true, '');
		$iMediaId = $this->input->post('iMediaId', true, 0);
		$iFromX = $this->input->post('iFromX', true, 0);
		$iFromY = $this->input->post('iFromY', true, 0); 
		$iWidth = $this->input->post('iWidth', true, 0);
		$iHeight = $this->input->post('iHeight', true, 0); 	


		if (substr($sSrcImage, 0, 1) === '/')
		{
			$sSrcImage = substr($sSrcImage, 1);	
		}
		
		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = 'O';
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 0)
		{
			// TODO: throw an error - media not found
			return;
		}

		$oMedia = $qMedia->row(0);
		
		$sDestFolder = $this->config->item('admin_media_tmp_dir_clean');
		
		$sDestFolder .= $this->user->userId.'_'.$iMediaId;
		
		if (file_exists($sDestFolder) === false) 
		{
			mkdir($sDestFolder);
			@chmod($sDestFolder, 0777);	
		}
		
		$sDestImage = random_string('unique').'.'.$oMedia->extension_ext;
		
		
		$sDestImage = $sDestFolder.'/'.$sDestImage;
		
		
		$this->load->library('image_lib');
		
		$this->image_lib->clear();
		
		$config['source_image'] = $sSrcImage;
		$config['x_axis'] = $iFromX;
		$config['y_axis'] = $iFromY;
		$config['width'] = $iWidth;
		$config['height'] = $iHeight;
		$config['maintain_ratio'] = false;
		$config['new_image'] = $sDestImage;
		
		
		$this->image_lib->initialize($config);
		
		
		
		if (!$this->image_lib->crop())
		{
			$this->return_image_lib_errors();
			return;
		} 

		$sContent = '/'.$sDestImage;
		$sHistory = $this->aData['aMessages']['admin_edit_media_image_cropped'].' '.$iWidth.' x '.$iHeight;	

		$sContent = $this->jsonify_string($sContent);
		$sHistory = $this->jsonify_string($sHistory);
		
		$this->aData['json_encode'] = array('sContent' => $sContent, 'sHistory' => $sHistory);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function get_image_brightness() 
	{
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/media/edit_tools/image_brightness', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function apply_image_brightness() 
	{
		$sSrcImage = $this->input->post('sSrcImage', true, '');
		$iMediaId = (int) $this->input->post('iMediaId', true, 0);
		$iAdjust = (int) $this->input->post('iAdjust', true, 0);		
		
		


		if (substr($sSrcImage, 0, 1) === '/')
		{
			$sSrcImage = substr($sSrcImage, 1);	
		}

		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = 'O';
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 0)
		{
			// TODO: throw an error - media not found
			return;
		}

		$oMedia = $qMedia->row(0);		
		
		$sDestFolder = $this->config->item('admin_media_tmp_dir_clean');
		
		$sDestFolder .= $this->user->userId.'_'.$iMediaId;
		
		if (file_exists($sDestFolder) === false) 
		{
			mkdir($sDestFolder);
			@chmod($sDestFolder, 0777);	
		}
		
		$sDestImage = random_string('unique').'.'.$oMedia->extension_ext;
		
		
		$sDestImage = $sDestFolder.'/'.$sDestImage;
		
		
		$this->load->library('image_lib');
		
		$this->image_lib->clear();
		
		$config['source_image'] = $sSrcImage;
		$config['new_image'] = $sDestImage;
		$config['adjustment'] = $iAdjust;
		
		$this->image_lib->initialize($config);
		
		if (!$this->image_lib->brightness())
		{
			$this->return_image_lib_errors();
			return;
		} 

		$sContent = '/'.$sDestImage;
		$sHistory = $this->aData['aMessages']['admin_edit_media_image_brightness'].' '.$iAdjust.'&#37;';
		
		$aImageSize = getimagesize($sDestImage);

		$sContent = $this->jsonify_string($sContent);
		$sHistory = $this->jsonify_string($sHistory);
		$iWidth = $aImageSize[0];
		$iHeight = $aImageSize[1];
		
		$this->aData['json_encode'] = array('sContent' => $sContent, 'sHistory' => $sHistory, 'iWidth' => $iWidth, 'iHeight' => $iHeight);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function get_image_contrast() 
	{
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/media/edit_tools/image_contrast', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function apply_image_contrast() 
	{
		$sSrcImage = $this->input->post('sSrcImage', true, '');
		$iMediaId = (int) $this->input->post('iMediaId', true, 0);
		$iAdjust = (int) $this->input->post('iAdjust', true, 0);		
		
		


		if (substr($sSrcImage, 0, 1) === '/')
		{
			$sSrcImage = substr($sSrcImage, 1);	
		}

		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = 'O';
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 0)
		{
			// TODO: throw an error - media not found
			return;
		}

		$oMedia = $qMedia->row(0);		
		
		$sDestFolder = $this->config->item('admin_media_tmp_dir_clean');
		
		$sDestFolder .= $this->user->userId.'_'.$iMediaId;
		
		if (file_exists($sDestFolder) === false) 
		{
			mkdir($sDestFolder);
			@chmod($sDestFolder, 0777);	
		}
		
		$sDestImage = random_string('unique').'.'.$oMedia->extension_ext;
		
		
		$sDestImage = $sDestFolder.'/'.$sDestImage;
		
		
		$this->load->library('image_lib');
		
		$this->image_lib->clear();
		
		$config['source_image'] = $sSrcImage;
		$config['new_image'] = $sDestImage;
		$config['adjustment'] = $iAdjust;
		
		$this->image_lib->initialize($config);
		
		if (!$this->image_lib->contrast())
		{
			$this->return_image_lib_errors();
			return;
		} 

		$sContent = '/'.$sDestImage;
		$sHistory = $this->aData['aMessages']['admin_edit_media_image_contrast'].' '.$iAdjust.'%';
		
		$aImageSize = getimagesize($sDestImage);

		$sContent = $this->jsonify_string($sContent);
		$sHistory = $this->jsonify_string($sHistory);
		$iWidth = $aImageSize[0];
		$iHeight = $aImageSize[1];
		
		$this->aData['json_encode'] = array('sContent' => $sContent, 'sHistory' => $sHistory, 'iWidth' => $iWidth, 'iHeight' => $iHeight);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function get_image_resize() 
	{
		$sContent = $this->load->view('platform/'.$this->sPlatform.'/admin/media/edit_tools/image_resize', $this->aData, true);
		
		$sContent = $this->jsonify_string($sContent);
		
		$this->aData['json_encode'] = array('sContent' => $sContent);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function apply_resize() 
	{
		$sSrcImage = $this->input->post('sSrcImage', true, '');
		$iMediaId = (int) $this->input->post('iMediaId', true, 0);
		$iWidth = $this->input->post('iWidth', true, 0);
		$iHeight = $this->input->post('iHeight', true, 0); 		
		
		


		if (substr($sSrcImage, 0, 1) === '/')
		{
			$sSrcImage = substr($sSrcImage, 1);	
		}

		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = 'O';
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 0)
		{
			// TODO: throw an error - media not found
			return;
		}

		$oMedia = $qMedia->row(0);		
		
		$sDestFolder = $this->config->item('admin_media_tmp_dir_clean');
		
		$sDestFolder .= $this->user->userId.'_'.$iMediaId;
		
		if (file_exists($sDestFolder) === false) 
		{
			mkdir($sDestFolder);
			@chmod($sDestFolder, 0777);	
		}
		
		$sDestImage = random_string('unique').'.'.$oMedia->extension_ext;
		
		
		$sDestImage = $sDestFolder.'/'.$sDestImage;
		
		
		$this->load->library('image_lib');
		
		$this->image_lib->clear();
		
		$config['source_image'] = $sSrcImage;
		$config['new_image'] = $sDestImage;
		$config['width'] = $iWidth;
		$config['height'] = $iHeight;
		
		$this->image_lib->initialize($config);
		
		if (!$this->image_lib->resize())
		{
			$this->return_image_lib_errors();
			return;
		} 

		$sContent = '/'.$sDestImage;
		$sHistory = $this->aData['aMessages']['admin_edit_media_image_resized'].' '.$iWidth.' x '.$iHeight;
		
		$aImageSize = getimagesize($sDestImage);

		$sContent = $this->jsonify_string($sContent);
		$sHistory = $this->jsonify_string($sHistory);
		$iWidth = $aImageSize[0];
		$iHeight = $aImageSize[1];
		
		$this->aData['json_encode'] = array('sContent' => $sContent, 'sHistory' => $sHistory, 'iWidth' => $iWidth, 'iHeight' => $iHeight);
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	public function image_rotate_left() 
	{
		$this->image_rotation(90, $this->aData['aMessages']['admin_edit_media_image_rotated_l']);
		return;
	}
	
	public function image_rotate_right() 
	{
		$this->image_rotation(270, $this->aData['aMessages']['admin_edit_media_image_rotated_r']);
		return;
	}
	
	
	public function image_flip_h()
	{
		$this->image_rotation('hor', $this->aData['aMessages']['admin_edit_media_image_flipped_h']);
		return;
	}
	
	
	public function image_flip_v()
	{
		$this->image_rotation('vrt', $this->aData['aMessages']['admin_edit_media_image_flipped_v']);
		return;
	}
	
	
	
	private function image_rotation($vRotation, $sHistory)
	{
		$sSrcImage = $this->input->post('sSrcImage', true, '');
		$iMediaId = $this->input->post('iMediaId', true, 0);
		
		if (substr($sSrcImage, 0, 1) === '/')
		{
			$sSrcImage = substr($sSrcImage, 1);	
		}
		
		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = 'O';
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 0)
		{
			// TODO: throw an error - media not found
			return;
		}

		$oMedia = $qMedia->row(0);
		
		$sDestFolder = $this->config->item('admin_media_tmp_dir_clean');
		
		$sDestFolder .= $this->user->userId.'_'.$iMediaId;
		
		if (file_exists($sDestFolder) === false) 
		{
			mkdir($sDestFolder);
			@chmod($sDestFolder, 0777);	
		}
		
		$sDestImage = random_string('unique').'.'.$oMedia->extension_ext;
		
		
		$sDestImage = $sDestFolder.'/'.$sDestImage;
		
		
		$this->load->library('image_lib');
		
		$this->image_lib->clear();
		
		$config['source_image'] = $sSrcImage;
		$config['new_image'] = $sDestImage;
		$config['rotation_angle'] = $vRotation;
		
		$this->image_lib->initialize($config);
		
		
		
		if (!$this->image_lib->rotate())
		{
			$this->return_image_lib_errors();
			return;
		} 

		$sContent = '/'.$sDestImage;
		
		$aImageSize = getimagesize($sDestImage);

		$sContent = $this->jsonify_string($sContent);
		$sHistory = $this->jsonify_string($sHistory);
		$iWidth = $aImageSize[0];
		$iHeight = $aImageSize[1];
		
		$this->aData['json_encode'] = array('sContent' => $sContent, 'sHistory' => $sHistory, 'iWidth' => $iWidth, 'iHeight' => $iHeight);
		
		$this->load->view('php_json_encode', $this->aData);
	
	}
	
	
	public function remove_tmp() 
	{
		$iMediaId = $this->input->post('iMediaId', true, 0);
		
		$sFolder = $this->config->item('admin_media_tmp_dir_clean').$this->user->userId.'_'.$iMediaId;
		
		if ((file_exists($sFolder) === true) && (is_dir($sFolder) === true))
		{
			if(!$dirHandle = @opendir($sFolder)) 
			{
				return;
			}
    		
			while (false !== ($obj = readdir($dirHandle))) 
			{
	        
				if($obj=='.' || $obj=='..') 
				{	
					continue;
				}

		        @unlink($sFolder.'/'.$obj);
		    }

		}
		
		
		$this->aData['json_encode'] = array('sDone' => 'Y');
		
		$this->load->view('php_json_encode', $this->aData);
	}
	
	
	
	public function save_tmp()
	{
		$sSrcImage = $this->input->post('sSrcImage', true, '');
		$iMediaId = $this->input->post('iMediaId', true, 0);
		
		if (substr($sSrcImage, 0, 1) === '/')
		{
			$sSrcImage = substr($sSrcImage, 1);	
		}
		
		$this->load->model('db/views/media_by_folders', 'vw_media_by_folders');
		
		$this->vw_media_by_folders->media_id = $iMediaId;
		$this->vw_media_by_folders->dimension_type = 'W';
		
		$qMedia = $this->vw_media_by_folders->get(array(), 1);
		
		if ($qMedia->num_rows() === 0)
		{
			// TODO: throw an error - media not found
			return;
		}

		$oMedia = $qMedia->row(0);
		
		
		$this->load->library('media_lib');
		
		$this->media_lib->add_file_to_media($sSrcImage, $oMedia->extension_ext, $oMedia->media_title, $oMedia->folder_id); 
		
		
		$this->aData['json_encode'] = array('sDone' => 'Y');
		
		$this->load->view('php_json_encode', $this->aData);	
		
		return;
		
		$sMediaDir = $this->config->item('admin_media_dir_clean');
		$aMediaTypes = $this->config->item('admin_media_file_dims');
		// attempt to unlink this particular file
		$sFile = $sMediaDir.$aMediaTypes[$oMedia->dimension_type][0].'/'.$oMedia->media_subfolder.'/'.$oMedia->media_id.'.'.$oMedia->extension_ext;
		

		copy($sSrcImage, $sFile);
		
		
		$aImageSize = getimagesize($sFile);
		
		$this->load->model('db/media_dimensions', 'media_dimensions');
		
		$this->media_dimensions->media_id = $oMedia->media_id;
		$this->media_dimensions->dimension_type = $oMedia->dimension_type;
		
		$qDimensions = $this->media_dimensions->get(array(), 1);

		
		if ($qDimensions->num_rows() === 1)
		{
			$oDimensions = $qDimensions->row(0);
			
			$this->media_dimensions->clear();
			
			$this->media_dimensions->dimension_id = $oDimensions->dimension_id;	
			$this->media_dimensions->dimension_width = $aImageSize[0];
			$this->media_dimensions->dimension_height = $aImageSize[1];	
			
			$this->media_dimensions->save();
		

			
			
			$iFileSize = filesize($sFile);
			
			
			$this->load->model('db/media', 'media');
			
			$this->media->media_id = $oDimensions->media_id;
			$this->media->media_size = $iFileSize;
			
			$this->media->save();
		
			list($sMediaTypeText, $iMediaMaxWidth, $iMediaMaxHeight) = $aMediaTypes['T'];
			
			$sThumb = $sMediaDir.$sMediaTypeText.'/'.$oMedia->media_subfolder.'/'.$oMedia->media_id.'.'.$oMedia->extension_ext; 
		
			// now we need to regenerate the thumbnail
			$this->load->library('image_lib');
			
			$config['image_library'] = 'gd2';
			$config['source_image'] = $sFile;
			$config['new_image'] = $sThumb;
			$config['create_thumb'] = false;
			$config['maintain_ratio'] = true;
			$config['width'] = $iMediaMaxWidth;
			$config['height'] = $iMediaMaxHeight;
			$config['master_dim'] = 'auto';
			
			
			$this->image_lib->initialize($config);
	
			if (!$this->image_lib->resize()) {
				
			
			}
			$this->image_lib->clear();
		}
		
		
		$this->aData['json_encode'] = array('sDone' => 'Y');
		
		$this->load->view('php_json_encode', $this->aData);	

	}
}

?>
